package cn.zpcnet.zstring.utils.read;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

import cn.zpcnet.zstring.utils.exception.StringConvertNumberException;

/**
 * @author zpc20
 * @version 1.0
 */
public class ReadFromConsole implements ReadScanner {
    private static final Scanner scan = new Scanner(System.in);
    private static String delimeter = ",";
    
	@Override
	public String readLine() {
		if(scan.hasNextLine()) {
			return scan.nextLine();
		}
		return null;
	}


	@Override
	public int readInt() throws StringConvertNumberException {
		if(scan.hasNextInt()) {
			return scan.nextInt();
		}
		throw new StringConvertNumberException("请输入数字");
	}

	@Override
	public Scanner getScannerInstance() {
		return scan;
	}


	@Override
	public void close() {
		scan.close();	
	}
	
	public static  String  sortStringNumber() throws StringConvertNumberException {
		final List<Integer> list = new ArrayList<>();
		if(scan.hasNextLine()) {
		  try{
			 Arrays.stream(scan.nextLine()
				   .split(delimeter))
			       .forEach(
					 (item)->{
					   list.add(Integer.parseInt(item));     	
				   });
		  }catch(NumberFormatException e){
			  throw new StringConvertNumberException("请输入数字",e);
		  }
		}  
		Collections.sort(list);
		return list.toString();
	}
	
}
